
import { loginWxAction } from "../../../../service/config"

import {
    articleDetail
} from "../../../../service/jb_article_service"
import {
    BIND_USER_ID
} from "../../../../service/base_service"
import {
    VIDEO_COVER,
} from "../../../../service/base_service"
import {
    queryWorksVoteRecord,
    worksDetailReq,
    checkWorksReq,
} from "../../../../service/jb_activity_service"
let app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        menuTop: app.globalData.menuButton.top,
        menuHeight: app.globalData.menuButton.height,
        videoCoverUrl: VIDEO_COVER,
        videoUrl: "",
        id: "",
        isShare: false,
        userId: "",
        bindUserId: "",
        shareName: "",
        knoData: {
            title: "",
            synopsis: "",
            imageInput: '',
        },
        detailInfo: {},//详情
        userInfo: {},
        productDetail: {},
        worksId: null,
        dataList: [1, 2, 3, 4, 5, 6, 8],
        page: 1,
        limit: 10,
        isShowLoading: false,
        isBottomShow: false,
        refreshFlag: false,
        actionType: 0, //0:自己预览，1：oa审核预览 2：模版预览
        showRefundPop: false,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (JSON.stringify(app.globalData.worksPreview) != "{}") {
            this.setData({
                detailInfo: app.globalData.worksPreview,
            })
            // app.globalData.worksPreview = {};
        }
        this.setData({
            actionType: options.action,
            userInfo: app.globalData.userInfo,
        })
        if (this.data.actionType != 2) {
            this.queryWorksDetail();
        }


    },

    queryWorksDetail() {
        wx.showLoading()
        worksDetailReq(this.data.detailInfo.id).then(res => {
            wx.hideLoading()
            if (res.code == 200) {
                this.setData({
                    detailInfo: res.data
                })
                if (res.data.products && res.data.products.length > 0) {
                    this.setData({
                        productDetail: res.data.products[0],
                    })
                }
            }
        }).catch(err => {
            wx.hideLoading()
            wx.showToast({
                title: err || '查询失败',
                icon: "none"
            })
            console.error("JJLog:worksDetailReq", err);
        })
    },


    queryKnoDetail() {

        wx.showLoading();

        articleDetail(this.data.id).then(res => {
            wx.hideLoading();
            if (res.code == 200) {
                this.setData({
                    knoData: res.data,

                })
            }
        }).catch(err => {
            if (this.data.loading) {
                wx.hideLoading();
            }
        })
    },
    //跳转到商品详情
    pushToProduct() {
        wx.navigateTo({
            url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?houseNumber=' + this.data.knoData.products[0].id + "&type=normal",
        })
    },

    //用户登陆授权等:
    // userWxLogin() {
    //   loginWxAction().then((res => {
    //     console.log("JJLog:登录返回:" + JSON.stringify(res));
    //     let data = res.data.data;

    //     if (res.code == 200) {

    //       app.globalData.nickName = data.nickName;
    //       app.globalData.avatar = data.avatar;
    //       app.globalData.name = data.name || "";
    //       app.globalData.phone = data.phone || "";
    //       app.globalData.isLogin = data.isLogin;
    //       app.globalData.userId = data.id;
    //       app.globalData.qrCode = data.qrCode || "";
    //       app.globalData.isAuthUserInfo = data.isAuthUserInfo;
    //       app.globalData.weChat = data.weChat || "";
    //       app.globalData.roleName = data.roleName || "";
    //       app.globalData.companyName = data.companyName || "";
    //       app.globalData.companyNumber = data.companyNumber || "";
    //       app.globalData.isBindMp = data.isBindMp;
    //       app.globalData.roleType = data.roleType || 1


    //       //绑定挂级关系:
    //       if (this.data.bindUserId) {
    //         this.bindUserRelation();
    //       }



    //     }
    //   })).catch(err => {
    //     console.error("JJLog:userWxLogin", err);

    //   })
    // },


    //视频播放失败回调;
    videoErrorCallback(e) {
        console.log("JJLog:视频播放失败:" + JSON.stringify(e));
    },
    //缓存中...
    videoWaiting(e) {
        console.log("JJLog:缓存zhong：" + JSON.stringify(e));
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },




    backHomeAction() {
        wx.switchTab({
            url: '/pages/home/home',
        })
    },

    //获取投票记录
    queryWorksVoteRecord() {
        wx.showLoading();
        queryWorksVoteRecord(this.data.page, this.data.limit, this.data.detailInfo.voteId, this.data.worksId).then(res => {
            wx.hideLoading();
            if (res.code == 200 && res.data) {
                let arr = (type == 'refresh' ? [] : this.data.dataList || []);
                arr = arr.concat(res.data.list || [])
                this.setData({
                    dataList: arr,
                    refreshFlag: false,
                    isShowLoading: false,
                    isBottomShow: res.data.totalPage == this.data.page ? true : false,
                })

            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '加载失败',
                icon: "none"
            })
            if (this.data.page != 1) {
                this.setData({
                    page: this.data.page - 1
                })
            }
            this.setData({
                refreshFlag: false,
            })
            console.error('JJLog:queryWorksVoteRecord', err);
        })
    },

    // 拒绝作品通过
    refuseWorksAction() {

        this.setData({
            showRefundPop: true,
        })
    },
    //同意作品通过
    agreeWorksAction() {
        wx.showModal({
            title: '提示',
            content: '确认作品通过审核?',
            confirmColor: "#ff0000",
            success: res => {
                if (res.confirm) {
                    let req = {
                        voitId: this.data.detailInfo.voteId,
                        id: this.data.detailInfo.id,
                        examineStatus: 1,
                    }
                    wx.showLoading();
                    checkWorksReq(req).then(res => {
                        wx.hideLoading();
                        if (res.code == 200) {
                            wx.showToast({
                                title: '操作成功',
                                icon: "success"
                            })
                            setTimeout(() => {
                                wx.navigateBack();
                            }, 2000)
                        }
                    }).catch(err => {
                        wx.hideLoading();
                        wx.showToast({
                            title: err || "操作失败",
                            icon: "none"
                        })
                        console.error("JJLog:checkWorksReq", err);
                    })
                }

            }
        })

    },
    //确认拒绝原因
    refundSureAction(e) {
        let valueText = e.detail.comment;
        let req = {
            voitId: this.data.detailInfo.voteId,
            id: this.data.detailInfo.id,
            examineStatus: 2,
            examineRemark: valueText,
        }
        wx.showLoading();
        checkWorksReq(req).then(res => {
            wx.hideLoading();
            if (res.code == 200) {
                wx.showToast({
                    title: '操作成功',
                    icon: "success"
                })
                this.setData({
                    showRefundPop: false
                })
                setTimeout(() => {
                    wx.navigateBack();
                }, 2000)
            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || "操作失败",
                icon: "none"
            })
            console.error("JJLog:checkWorksReq", err);
        })
    },
    refundPopHide() {
        this.setData({
            showRefundPop: false,
        })
    },
    pushToProduct() {
        wx.navigateTo({
            url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?houseNumber=' + this.data.productDetail.id + "&type=normal",
        })
    },



})